package com.chagee.store.operation.server.service.biz;

import cn.hutool.core.collection.CollectionUtil;
import com.chagee.base.utils.PaginationData;
import com.chagee.base.utils.PaginationRequest;
import com.chagee.scm.api.supplier.request.SearchSupplierBaseBO;
import com.chagee.scm.api.supplier.response.SupplierBaseBO;
import com.chagee.store.operation.api.response.SearchSupplierResponse;
import com.chagee.store.operation.common.constant.CommonConstant;
import com.chagee.store.operation.common.utils.CommonUtil;
import com.chagee.store.operation.server.service.third.ScmThirdService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springblade.core.tool.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author : hk
 * @Date : 2025/4/18
 */

@Slf4j
@Service
@RequiredArgsConstructor
public class SupplierDetailBizService {

	@Resource
	private ScmThirdService scmThirdService;

	@Value("${supplier.supplierCategoryId}")
	private String supplierCategoryId;


	public List<SearchSupplierResponse> supplierList() {
		SearchSupplierBaseBO searchSupplierBaseBO = new SearchSupplierBaseBO();
		searchSupplierBaseBO.setChannelType(CommonConstant.INTEGER_THREE);
		searchSupplierBaseBO.setSupplierCategoryId(Long.parseLong(supplierCategoryId));

		PaginationRequest<SearchSupplierBaseBO> paginationRequest = CommonUtil.convertPageRequest(searchSupplierBaseBO, CommonConstant.DELETE_FLAG, CommonConstant.PAGE_SIZE_MAX);

		PaginationData<SupplierBaseBO> supplierPageData = scmThirdService.searchSupplier(paginationRequest);

		if (Objects.isNull(supplierPageData) || CollectionUtils.isEmpty(supplierPageData.getPageList())) {
			return CollectionUtil.newArrayList();
		}

		List<SearchSupplierResponse> responseList = supplierPageData.getPageList().stream().map(s -> {
			SearchSupplierResponse supplierResponse = new SearchSupplierResponse();
			BeanUtil.copyProperties(s, supplierResponse);
			return supplierResponse;
		}).collect(Collectors.toList());

		return responseList;
	}
}
